<?php
namespace app\admin\controller;

use app\BaseController;

use app\common\model\FirmAccountModel;
use app\common\model\AccountModel;
class Account extends BaseController{
    
    public function list(){
        $where = [
            ['firm_id','=',$this->request->firm_id]
        ];
        if($this->request->role < 5){
            $where[] = ['is_delete','=',0];
        }
        if($this->request->role == 0){
            $where[] = ['create_account_id','=',$this->request->account_id];
        }
        $list = FirmAccountModel::with('account')
            ->where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('firm_account_id','desc')
            ->select()
            ->append(['account']);
        $total = FirmAccountModel::where($where)->count('firm_account_id');
        return success([
            'list' => $list,
            'total' => $total,
        ]);
        
        $keyword = $this->request->param('keyword/s','');
        $where = AccountModel::where('name|mobile','like','%'.$keyword.'%');
        // return success($where);
        $list = FirmAccountModel::hasWhere('account', $where)
            ->where('firm_id', $this->request->firm_id)
            ->where('is_delete',0)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('firm_account_id','desc')
            ->select()
            ->append(['account']);
        $total = FirmAccountModel::hasWhere('account', $where)->where('firm_id', $this->request->firm_id)->where('is_delete',0)->count('firm_account_id');
        return success([
            'list' => $list,
            'total' => $total,
        ]);
    }
    
    public  function detail(){
        $firm_account_id = $this->request->param('firm_account_id/d',0);
        if(empty($firm_account_id)){
            return error('错误操作');
        }
        if(null === $firmAccountModel = FirmAccountModel::find($firm_account_id)){
            return error('内容不存在');
        }
        if($firmAccountModel->firm_id != $this->request->firm_id){
             return error('没有权限');
        }
        return success($firmAccountModel);
    }
    
    public function save(){

        $mobile = $this->request->param('mobile/s','');
        $name = $this->request->param('name/s','');

        if(empty($mobile)){
            return error('请填写手机号');
        }

        // if(!is_phone($mobile)){
        //     return error('手机号格式错误');
        // }

        if(empty($name)){
            return error('请填写名称');
        }

        $account_id = AccountModel::where('mobile',$mobile)->value('account_id');

        if(empty($account_id)){
            $accountModel = new AccountModel();
            $accountModel->mobile = $mobile;
            $accountModel->name = $name;
            $accountModel->pwd = 123456;
            $accountModel->save();
            $account_id = $accountModel->account_id;
        }

        $firmAccountModel = FirmAccountModel::where([
            'firm_id'=>$this->request->firm_id,
            'account_id'=>$account_id
        ])->find();

        if(empty($firmAccountModel)){
            $firmAccountModel = new FirmAccountModel();
            $firmAccountModel->firm_id = $this->request->firm_id;
            $firmAccountModel->account_id = $account_id;
            $firmAccountModel->create_account_id = $this->request->role < 2 ? $this->request->account_id : 0;
            $firmAccountModel->save();
        }elseif($firmAccountModel->is_delete == 1){
            $firmAccountModel->is_delete = 0;
            $firmAccountModel->create_account_id = $this->request->role < 2 ? $this->request->account_id : 0;
            $firmAccountModel->save();
        }else{
            return error('账号已存在');
        }

        return success('添加成功');
    }
    
    public function delete(){
        $firm_account_id = $this->request->param('firm_account_id/d',0);
        if(empty($firm_account_id)){
            return error('错误操作');
        }
        if(null === $firmAccountModel = FirmAccountModel::find($firm_account_id)){
            return error('账号不存在');
        }
        if($firmAccountModel->firm_id != $this->request->firm_id){
            return error('没有权限');
        }
        if($firmAccountModel->account_id == $this->request->account_id){
            return error('不能删除自己');
        }
        $firmAccountModel->is_delete = 1;
        $firmAccountModel->save();
        return success('删除成功');
    }
    
}